import { GetModelDataCommand, GetModelDetailsCommand } from "../command/model.command"
import modelStatusEnum from "../enum/modelStatus.enum"

export default {
  getData: (sdk, allFunc, dependencies) => {
    allFunc.addBinding(GetModelDataCommand, async () => {
      const [e, o, n, r] = await dependencies.getDependencies()
      return await (async (e, o, n, r) => {
        const a = { sid: "", sweeps: [], modelSupportsVr: !1 }
        try {
          const s = o.getSweepList(),
            i = e.model
          return await allFunc.makeModelData(i.sid, i.options.isVR, s, n, r, a), a
        } catch (e) {
          throw Error("no model currently loaded")
        }
      })(e, o, n, r)
    })
    sdk.addCommandCreator({ namespace: "Model", name: "getData", args: [] }, () => new GetModelDataCommand())
  },
  getDetails: (sdk, exportOBJ, allFunc, dependencies) => {
    dependencies.getDependencies().then(([e, o]) =>
      allFunc.addBinding(GetModelDetailsCommand, async () =>
        (function (e, o) {
          function n(e, t) {
            if (o.tryGetProperty(e, !0)) return t
          }
          const r = e.model
          return {
            sid: r.sid,
            name: n(exportOBJ.OptionsKey.DetailsModelName, r.details.name),
            presentedBy: n(exportOBJ.OptionsKey.PresentedBy, r.details.presentedBy),
            summary: n(exportOBJ.OptionsKey.DetailsSummary, r.details.summary),
            formattedAddress: n(exportOBJ.OptionsKey.DetailsAddress, r.details.formattedAddress),
            contactEmail: n(exportOBJ.OptionsKey.DetailsEmail, r.details.contact.email),
            contactName: n(exportOBJ.OptionsKey.DetailsName, r.details.contact.name),
            phone: n(exportOBJ.OptionsKey.DetailsPhone, r.details.contact.phone),
            formattedContactPhone: n(exportOBJ.OptionsKey.DetailsPhone, r.details.contact.formattedPhone),
            externalUrl: n(exportOBJ.OptionsKey.DetailsExternalUrl, r.details.externalUrl)
          }
        })(e, o)
      )
    )
    sdk.addCommandCreator({ namespace: "Model", name: "getDetails", args: [] }, () => new GetModelDetailsCommand())
  },
  Event: sdk => {
    sdk.addEnumToInterface({ namespace: "Model", name: "Event", values: modelStatusEnum })
  },
  onEvent: (sdk, exportOBJ, allFunc, dependencies) => {
    const r = { sid: "", sweeps: [], modelSupportsVr: !1 },
      a = [r]
    dependencies.getDependencies().then(n => {
      allFunc.subscribe(exportOBJ.ModelDataLoadedMessage, async t => {
        const [s, i, d] = n
        await allFunc.makeModelData(t.sid, t.vrSupported, s.getSweepList(), i, d, r), sdk.broadcast(modelStatusEnum.MODEL_LOADED, () => a)
      })
    })
  }
}
